package ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import blockencryption.CipherManager;

public class ImageDisplayer {
	private final Display display = new Display();
	private Shell shell;
	private Label label;
	Button decryptButton;
	Label statusBar;
	private Image image;
	Text keyText;

	/**
	 * displays a shell with an image specified by <code>imagePath</code> and
	 * controls to encrypt and decrypt the image
	 */
	public void displayImage(String imagePath) {
		shell = new Shell(display, SWT.SHELL_TRIM & (~SWT.RESIZE));
		Image lockIcon = new Image(display, "image/lock.ico");
		shell.setImage(lockIcon);

		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		shell.setLayout(layout);

		// =================== THE IMAGE ===================

		label = new Label(shell, SWT.BORDER);
		image = new Image(display, imagePath);
		label.setImage(image);
		GridData data = new GridData();
		data.horizontalSpan = 3;
		label.setLayoutData(data);
		label.pack();

		// --- initialise CiphterManager with image data ---

		ImageData imageData = image.getImageData();
		final CipherManager cipherManager = new CipherManager(this, imageData);

		// ====== BUTTON-ROW: Encrypt [ key ] Decrypt =======

		// ---------------- Encrypt -------------------
		Button encryptButton = new Button(shell, SWT.NONE);
		encryptButton.setText("Encrypt");
		encryptButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				cipherManager.cipher(true);
			}
		});
		encryptButton.pack();

		// ------------------ Key -----------------------
		keyText = new Text(shell, SWT.SINGLE | SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		keyText.setLayoutData(data);
		keyText.pack();

		// ----------------- Decrypt ---------------------
		decryptButton = new Button(shell, SWT.NONE);
		decryptButton.setText("Decrypt");
		decryptButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				cipherManager.cipher(false);
			}
		});
		decryptButton.setEnabled(false);
		decryptButton.pack();

		// ------------------- Status Bar ------------------
		// this displays a message if the key is wrong

		statusBar = new Label(shell, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 3;
		statusBar.setLayoutData(data);
		statusBar.pack();

		// ================= Operation code =================

		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	// ----------------- methods for interacting with UI -------------------

	public String getKey() {
		return keyText.getText();
	}

	public void setKey(String key) {
		keyText.setText(key);
	}

	public void setDecryptEnabled(boolean enabled) {
		decryptButton.setEnabled(enabled);
	}

	public void setStatus(String status) {
		statusBar.setText(status);
	}

	public void setImageData(int[][] data) {
		ImageData imageData = image.getImageData();
		for (int lineNo = 0; lineNo < data.length; lineNo++) {
			imageData.setPixels(0, lineNo, imageData.width, data[lineNo], 0);
		}
		image = new Image(display, imageData);
		label.setImage(image);
	}
}
